package com.wsx.five;

import java.util.Arrays;

/**
 * Created by wangshunxi on 2022/6/19.
 * e-mail: wsr12240512@163.com
 * phone: 13388071923
 */
public class Main0001 {
    public static void main(String[] args) {
        int[] arr = {2, 9, 10, 3, 7};
        int[] newArr = arr;
        while (true) {
            newArr = combin(newArr);
            if (!check(newArr)) {
                break;
            }
        }
        System.out.println(Arrays.toString(newArr));

    }

    private static int[] combin(int[] arr) {
        int max = Integer.MIN_VALUE;
        int[] ints = new int[2];
        for (int i = 0; i < arr.length - 1; i++) {
            if (arr[i] < arr[i + 1] && arr[i] + arr[i + 1] > max) {
                ints[0] = i;
                ints[1] = i + 1;
                max = arr[i] + arr[i + 1];
            }
        }
        int[] result = new int[arr.length - 1];
        for (int i = 0; i < ints[0]; i++) {
            result[i] = arr[i];
        }
        result[ints[0]] = max;
        for (int i = ints[1]+1; i < arr.length; i++) {
            result[i-1] = arr[i];
        }
        System.out.println(Arrays.toString(result));
        return result;
    }

    private static boolean check(int[] newArr) {
        for (int i = 0; i < newArr.length; i++) {
            for (int j = i + 1; j < newArr.length; j++) {
                if (newArr[i] < newArr[j]) {
                    return true;
                }
            }
        }
        return false;
    }
}
